//
//  WYStatusPictureView.swift
//  weibo
//
//  Created by 王玉东 on 16/4/13.
//  Copyright © 2016年 wyd. All rights reserved.
//

import UIKit
import SDWebImage

private let pictureCellId = "pictureId"
private let pictureViewMargin: CGFloat = 3
private let maxWidth = ScreenWidth - 2 * StatusCellMargin
private let itemWidth = (maxWidth - 2 * StatusCellMargin) / 3
class WYStatusPictureView: UICollectionView {
//图片之间的间隙
    var imageURLs:[WYStatusPicture]? {
        didSet{
        
        print(imageURLs?.count)
        //更新图片的大小
        let pSize = caculatePictureViewSize()
             self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(pSize)
            }
            testLable.text = "\(imageURLs?.count ?? 0)"
            //刷新数据
            self.reloadData()
           
            
        }
    }
    
private func caculatePictureViewSize() -> CGSize {
    
        let imageCount = imageURLs?.count ?? 0
        
        if imageCount == 0 {
            return CGSizeZero
        }
    let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        if imageCount == 1 {
                let imagesize = CGSize(width: 100, height: 150)
            
            
            
                layout.itemSize = imagesize
            
                return imagesize
    }

//        if imageCount == 2 {
//        let imageSize = CGSize(width: 100, height: 100)
//        return imageSize
//        }
    layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        if imageCount == 4 {
        
        let w = itemWidth * 2 + pictureViewMargin
            return CGSize(width: w, height: w)
        }
        let row = CGFloat((imageCount - 1)/3 + 1)
        let h = row * itemWidth + (row - 1) * pictureViewMargin
        return CGSize(width: maxWidth, height: h)
    
    }
    
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let layout = UICollectionViewFlowLayout()
       // layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        layout.minimumInteritemSpacing = pictureViewMargin
        layout.minimumLineSpacing = pictureViewMargin
        super.init(frame: frame, collectionViewLayout: layout)
        
        setUI()
        
        self.dataSource = self
        
        self.registerClass(pictureCell.self, forCellWithReuseIdentifier: pictureCellId)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private lazy var testLable:UILabel = UILabel(text: "", fontSize: 30, textColor: UIColor.redColor())
    private func setUI(){
    addSubview(testLable)
        testLable.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
    }
}

extension WYStatusPictureView:UICollectionViewDataSource {

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageURLs?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureCellId, forIndexPath: indexPath) as! pictureCell
        
        cell.statusPicture = imageURLs![indexPath.item]
        
        cell.backgroundColor = randomColor()
        return cell
        
        
    }

}



class pictureCell:UICollectionViewCell {
    
    var statusPicture:WYStatusPicture? {
        didSet{
        let url = NSURL(string: statusPicture?.thumbnail_pic ?? "")
            
         iconView.sd_setImageWithURL(url)
        
        }
    
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        SetUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func SetUI() {
    
    contentView.addSubview(iconView)
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView.snp_edges)
        }
    
    }
    
   
    private lazy var iconView: UIImageView = {
    let iv = UIImageView()
        
        iv.contentMode = .ScaleAspectFill
        
        iv.clipsToBounds = true
        
        return iv
    }()
    
    
}
